<?php
echo $this->Html->script('ckeditor/ckeditor');
$nodeType=$type;
if (empty($typeId)) {?>
    <div class="section">
        <?php echo $this->element('toolbar');
            if (isset($listTypes)) {
                foreach ($listTypes as $type) {
                    echo '<li>' . $this->Html->link($type['NodeType']['name'], array('action' => 'add', $type['NodeType']['id'])) . '</li>';
                }
            }
        ?>
    </div>
<?php } else { ?>
    <div class="page-add-new-article">
        <div id="content-header">
            <h1>Add new article</h1>
        </div>
        <div class="container-fluid">

         <div class="row-fluid">
          <div class="span12">
    <?php
        echo $this->Form->create('Node', array('id' => 'paginate','class'=>'form-horizontal', 'action' => 'edit', 'url' => $this->request->here(false), 'type' => 'file',
                'inputDefaults' => array('label' => false, 'format' => array('before', 'label', 'between', 'input', 'after', 'error'))));       
    ?>	
        <?php
            $fieldsets['node_type_id'] = $this->Form->input('node_type_id', array(
                    'label' => false,
                    'value' => $type['NodeType']['id'],
                    'type' => 'hidden'));
            $fieldsets['term_id'] = $this->Form->input('term_id', array(            
                    'type' => $term_type,
                    'options' => $list_terms,
                    'value' => $term_id,
                    'empty' => '-- None --',
                    'label' => array('class' => 'control-label'),
                    'between' => '<div class="wd-select wd-select-large controls ">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'escape' => false));
            $fieldsets['node_type_id'] = $this->Form->input('node_type_id', array(
                    'label' => array('class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'value' => $type['NodeType']['id'],
                    'type' => 'hidden'));

            $fieldsets['user_id'] = $this->Form->input('user_id', array(
                    'label' => array('class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'value' => $user['id'],
                    'type' => 'hidden'));

            $fieldsets['title'] = $this->Form->input('title', array(
                    'label' => array( 'text'=> 'Title','class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'type' => 'text'));        
            $fieldsets['is_published'] = $this->Form->input('is_published', array(
                    'label' => array('text'=> 'Show/Hide','class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'default'=>1,
                    'after' => '</div>',
                    'type' => 'checkbox'));

        ?>
        <?php
            $layout = array(
                array('node_type_id', 'user_id'),
                array('title','term_id'),      
                array('is_published'),

            );
            //---------------------------------------------//
            foreach ($fields as $field) {

                $type = 'text';
                if ($field['Field']['field_type'] == 'img' || $field['Field']['field_type'] == 'file') {
                    $type = 'file';
                }
                if($field['Field']['field_type'] == 'textarea'){
                    $type= 'textarea';
                }
                else if($field['Field']['field_type'] == 'checkbox'){
                    $type= 'checkbox';
                }
                $chosseckeditor = null;

                switch ($field['Field']['use_wyswyg']) {
                    case null:
                        $chosseckeditor =null;
                        break;
                    case 0:
                        $chosseckeditor =null;
                        break;                
                    case 1:
                        $chosseckeditor = 'ckeditor';
                        break;
                }  
                $fieldsets['field_' . $field['Field']['name']] = $this->Form->input('field_' . $field['Field']['name'], array(
                        'label' => array('text'=>$field['Field']['label'],'class' => 'control-label'),
                        'between' => '<div class="controls">',
                        'div' => array('class' => 'control-group'),
                        'after' => '</div>',
                        'class'=> $chosseckeditor,
                        'type' => $type));
                $layout[] = array('field_' . $field['Field']['name']);
            }

            $layout = array(
                array(
                    'Basic setting',
                    $layout,
                )
            );
            echo $this->Layout->form($layout, $fieldsets); 
        ?>    
        <?php if(isset($images) && !empty($images)){?>        
        <div class="widget-box widget-content-no-padding">
            
            <div class="control-group add-more">
                <label class="control-label" for="NodeFieldImage">Image</label>
                <div class="controls" >                    
                    <?php echo $this->Form->input('ProImage', array(
                                'class'=>'file-upload',
                                'label' => false,
                                'type'  => 'file', 
                                'name' => 'data[ProImage][0][path]'));?>
                    <span class="blue">Kích thước ảnh yêu cầu : 1300x500 </span>
                </div>
                <label class="control-label" for="NodeFieldImage">Hệ số ưu tiên</label>
                <div class="controls">
                    <?php echo $this->Form->input('ProImage', array(
                                'class'=>'',
                                'label' => false,
                                'type'  => 'text', 
                                'name' => 'data[ProImage][0][priority]'));?>
                </div>

            </div>	
            <div id="cloneImage"></div>
            <a style="padding-left: 200px;padding-bottom: 10px; clear: both; display: block;" href="javascript:add_more();"><?php echo __d("system", "Add more image")?></a>
        </div>                                                 
            <?php }?>  
            <div class="form-actions form-actions-two">
                <?php  echo $this->element('toolbar');?>
            </div>  
        <?php
            echo $this->Form->hidden('Paginate.task', array('id' => 'task', 'value' => ''));
            echo $this->Form->end();
        ?>              		
        						
    
    </div> 
    <?php }?>
<script type="text/javascript">
    jQuery(document).ready(function($){
            $('<span>Size: 1600x850</span>').insertAfter("#NodeFieldBackgroundImage");
            $('<span>Enter a valid number: (int)</span>').insertAfter("#NodeFieldBackgroundPiority");            
    });
    var counter=0;
    function add_more(){
    counter++;   
    var txt = '<label class="control-label" for="NodeFieldImage">Image</label><div class="controls"><div class="input text"><input name="data[ProImage]['+counter+'][path]" class="file-upload" type="text" id="NodeProImage" ><span class="blue">Kích thước ảnh yêu cầu : 1300x500 </span></div></div><label class="control-label" for="NodeFieldImage">Hệ số ưu tiên</label><div class="controls"><div class="input text"><input name="data[ProImage]['+counter+'][priority]" class="" type="text" id="NodeProImage"></div></div>';
        $(txt).appendTo('.add-more');
    var $this = Green.Elfinder;
        jQuery('form input.file-upload').prop('readonly' , true).click(function(){
                $this.show(jQuery(this));
        });                                        
             return false;
    }
    function delete_image(id){
        if (confirm("Are you sure you want to delete?")) {
            var href= "<?php echo '/node/node/delete_image/'?>"+id;
            jQuery.ajax({
                    url: href,
                    type: "GET",
                    success: function(data) {
                        jQuery('#image_'+id).remove();
                        jQuery('#image_del_'+id).remove();
                        //alert('Load was performed.');
                    }
            });
        }
    }
</script>
<style type="text/css">
    .wd-group-user,.wd-group-checked{border:none;border-bottom: 1px solid #ccc;border-top: 1px solid #ccc;padding-top: 0px;}
</style>